package com.ogawa.joypal.user.activity

import android.view.KeyEvent
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.ToastUtils
import com.ogawa.base.base.BaseVMActivity
import com.ogawa.projectcommon.bean.ProjectUserInfoBean
import com.ogawa.projectcommon.constants.UrlPathConstant
import com.ogawa.projectcommon.utils.ProjectSpUtils
import com.ogawa.base.utils.statusbar.SystemStatusBarUtil
import com.ogawa.joypal.user.R
import com.ogawa.joypal.user.viewmodel.LoginViewModel
import com.ogawa.projectcommon.constants.Constant.AGREE_MENT_URL
import com.yanzhenjie.permission.AndPermission
import com.yanzhenjie.permission.runtime.Permission
import kotlinx.android.synthetic.main.activity_login.*


@Route(path = UrlPathConstant.ACTIVITY_USER_LOGIN)
class LoginActivity : BaseVMActivity<LoginViewModel>() {
    override fun getLayoutId(): Int {
        return R.layout.activity_login
    }
    override fun providerVMClass(): Class<LoginViewModel>? {
        return LoginViewModel::class.java
    }

    override fun initView() {
        super.initView()

        AndPermission.with(this)
            .runtime()
            .permission(Permission.Group.STORAGE)
            .onGranted { }
            .onDenied { }
            .start()
        SystemStatusBarUtil.setStatusBar(this, R.color.colorWhite, true)
        tv_skip.setOnClickListener {
            var userInfoBean = ProjectUserInfoBean()
            userInfoBean.user = ProjectUserInfoBean.UserBean()
            userInfoBean.user.userName = getString(R.string.visitor_type)
            userInfoBean.user.id = "visitor"
            userInfoBean.isVisitor = true
            ProjectSpUtils.setUserInfo(userInfoBean)
            ARouter.getInstance().build(UrlPathConstant.ACTIVITY_DEVICEMANAGE_MAIN)
                .navigation()
            finish()
        }
        tv_get_code.setOnClickListener {
//            if (!cb_agree.isChecked){
//                ToastUtils.showShort(getString(R.string.please_read_user_agreement))
//                return@setOnClickListener
//            }
            val phoneStr = et_phone.text.toString()

            if (phoneStr.isBlank()){
                ToastUtils.showShort(getString(R.string.input_phone_num))
                return@setOnClickListener
            }

            if (phoneStr.length < 6){
                ToastUtils.showShort(getString(R.string.input_phone_num_len_less_err))
                return@setOnClickListener
            }
            mViewModel.getLoginBean(phoneStr)
        }
        tv_login.setOnClickListener {
            if (!cb_agree.isChecked){
                ToastUtils.showShort(getString(R.string.please_read_user_agreement))
                return@setOnClickListener
            }
            val phoneStr = et_phone.text.toString()
            if (et_code.text.isBlank()){
                ToastUtils.showShort(getString(R.string.input_code))
                return@setOnClickListener
            }
            if (phoneStr.isBlank()){
                ToastUtils.showShort(getString(R.string.input_phone_num))
                return@setOnClickListener
            }
            mViewModel.userLogin(phoneStr, et_code.text.toString())
        }

        iv_logo.setOnClickListener {

        }
        tv_agreement.setOnClickListener {
            ARouter.getInstance().build(UrlPathConstant.ACTIVITY_WEB_VIEW)
                .withString("titleStr", resources.getString(R.string.agreement))
                .withString("url", AGREE_MENT_URL)
                .navigation()
        }
        privacyPolicyTv.setOnClickListener {
            ARouter.getInstance().build(UrlPathConstant.ACTIVITY_USER_PRIVACY_POLICY)
                .navigation()
        }

    }


    var backTime: Long = 0
    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            val time = System.currentTimeMillis() - backTime
            backTime = System.currentTimeMillis()
            if (time < 2000 * 1) {
                AppUtils.exitApp()
                finish()
            } else {
                ToastUtils.showShort(getString(R.string.ret_back_app))
            }
            return true
        }
        return super.onKeyUp(keyCode, event)
    }


    override fun observeVM() {
        mViewModel.getSmsCountNumber.observe(this, Observer {
            if (it> 0){
                tv_get_code.isEnabled = false
                tv_get_code.text = "${it}s"
            }else{
                tv_get_code.isEnabled = true
                tv_get_code.text = getString(R.string.get_code)
            }
        })
        mViewModel.mUsercaptchBean.observe(this, Observer {
            ToastUtils.showShort(getString(R.string.sms_send_success))
        })
        mViewModel.mLoginBean.observe(this, Observer {
            val userInfoBean = ProjectUserInfoBean()
            val user = ProjectUserInfoBean.UserBean()
            it?.run {
                it.user?.run {
                    user.id = "${it.user.id}"
                    user.userName = it.user.userName
                    user.mobile = it.user.mobile
                    user.email = "${it.user.email}"
                    user.platform = it.user.platform
                }
                userInfoBean.user = user
                userInfoBean.token = it.token
                userInfoBean.loginType = it.loginType
                userInfoBean.isFirstLogin = it.isFirstLogin
                userInfoBean.userInfo = it.userInfo
                userInfoBean.region = it.region
                userInfoBean.loginType = it.loginType

                ProjectSpUtils.setUserInfo(userInfoBean)

                if (it.isFirstLogin){
                    ARouter.getInstance().build(UrlPathConstant.ACTIVITY_USER_REGISTER)
                        .navigation()
                }else{
                    ARouter.getInstance().build(UrlPathConstant.ACTIVITY_DEVICEMANAGE_MAIN)
                        .navigation()
                }
                finish()
            }
        })
    }
}